package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Goods;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品管理 Mapper 接口
 * </p>
 */
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {


    // 查询所有商品
    @Select("SELECT * FROM goods WHERE g_is_active = TRUE")
    List<Goods> selectAllGoods();

    // 根据分类查询商品
    @Select("SELECT * FROM goods WHERE g_category = #{category} AND g_is_active = TRUE")
    List<Goods> selectGoodsByCategory(@Param("category") String category);

    // 根据ID查询商品详情
    @Select("SELECT * FROM goods WHERE g_id = #{id}")
    Goods selectGoodsById(@Param("id") Integer id);

    // 查询所有分类
    @Select("SELECT DISTINCT g_category FROM goods")
    List<String> selectAllCategories();

    // 查询用户发布的商品
    @Select("SELECT * FROM goods WHERE g_user_id = #{userId}")
    List<Goods> selectGoodsByUserId(@Param("userId") Integer userId);

    @Select("<script>" +
            "SELECT * FROM goods " +
            "<where>" +
            "   <if test='keyword != null and keyword != \"\"'>" +
            "       g_name LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR g_category LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR g_description LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR g_create_time LIKE CONCAT('%', #{keyword}, '%') " +
            "       OR g_price LIKE CONCAT('%', #{keyword}, '%') " +
            "   </if>" +
            "</where>" +
            "</script>")
    IPage<Goods> selectGoodsPage(Page<Goods> page, @Param("keyword") String keyword);
}
